package com.atguigu.atcrowdfunding.manager.controller;

import com.atguigu.atcrowdfunding.bean.Cert;
import com.atguigu.atcrowdfunding.manager.service.CertService;
import com.atguigu.atcrowdfunding.manager.service.CertTypeService;
import com.atguigu.atcrowdfunding.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xzt
 * @create 2021-03-14 14:23
 */
@Controller
@RequestMapping("/certtype")
public class CertTypeController {

    @Autowired
    private CertTypeService certTypeService;

    @Autowired
    private CertService certService;

    @RequestMapping("/index")
    public String index(Model model){

        //查询所有资质
        List<Cert> queryAllCert = certService.queryAllCert();
        model.addAttribute("allCert",queryAllCert);
        return "certtype/index";
    }

    @ResponseBody
    @RequestMapping("/getCertAccttypeList")
    public AjaxResult getCertAccttypeList(){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            //查询资质与账户类型之间的关系（表示之前给账户类型分配过资质）
            List<Map<String,Object>> certAccttypeList = certTypeService.queryCertAccttype();
            ajaxResult.setData(certAccttypeList);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping("/insertAcctTypeCert")
    public AjaxResult insertAcctTypeCert(Integer certid,String accttype){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            HashMap<String, Object> map = new HashMap<>();
            map.put("certid",certid);
            map.put("accttype",accttype);

            int count = certTypeService.insertAcctTypeCert(map);
            ajaxResult.setSuccess(true);
        }catch (Exception e){
            ajaxResult.setSuccess(false);
            e.printStackTrace();
        }
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping("/deleteAcctTypeCert")
    public AjaxResult deleteAcctTypeCert(Integer certid,String accttype){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            HashMap<String, Object> map = new HashMap<>();
            map.put("certid",certid);
            map.put("accttype",accttype);

            int count = certTypeService.deleteAcctTypeCert(map);
            ajaxResult.setSuccess(true);
        }catch (Exception e){
            ajaxResult.setSuccess(false);
            e.printStackTrace();
        }
        return ajaxResult;
    }

}
